var common = require("../../utils/Conmon.js");
var appData = getApp().globalData;
Page({

  /**
   * 页面的初始数据
   */

  data: {
    arrList: null,
    Currencys: null,
    Menus: null,
    UnionShopMode: true

  },

  setting: function() {
    wx.showActionSheet({
      itemList: ['收货地址', '帐号与安全', '退出当前账户'],
      success: function(res) {
        console.log(res.tapIndex);
        if (res.tapIndex === 0) {
          wx.navigateTo({
            url: '/pages/deliveryAddr/deliveryAddr'
          })
        }
        if (res.tapIndex === 1) {
          wx.navigateTo({
            url: '/pages/accountSecurely/accountSecurely'
          })
        }
        if (res.tapIndex === 2) {
          wx.removeStorageSync("UserInfo");
          wx.navigateTo({
            url: '/pages/login/login?pageUrl=' + encodeURIComponent(common.getCurrentPageUrlWithArgs())
          })
        }
      },
      fail: function(res) {
        console.log(res.errMsg)

      }
    })
  },

  bindData: function(that) {
    wx.request({
      url: appData.apiurl + "member/api/MemberCenter/MemberCenterIndexData",
      method: "GET",
      data: {
        userId: wx.getStorageSync('UserInfo').UserID,
        custId: appData.custID,
        isValid: 1,
        typeId: 1
      },
      header: {
        "content-type": "application/json",
        'X_ManuName': appData.manuName
      },
      success: function(res) {
        wx.hideLoading()
        if (res.statusCode == 200 && res.data.code == 1200) {
          var currs = JSON.parse(res.data.content.UserCurrencyNameSet);
          if (currs != null && currs.length > 0) {
            for (var i = 0; i < currs.length; i++) {
              var Amount = currs[i].NowAmount.toFixed(2);
              currs[i].Amount = Amount;
            }
          }
          var menuJson = JSON.parse(res.data.content.MemberMenu);
          var menus = new Array();
          var hasUnionstore = false;
          if (menuJson != null) {
            for (var i = 0; i < menuJson.length; i++) {
              if (menuJson[i].MenuUrl != "/pages/MyShop/MyShop") {
                menus.push(menuJson[i]);
              } else {
                hasUnionstore = true;
              }
            }
          }

          that.setData({
            arrList: res.data.content,
            Currencys: currs,
            Menus: menus
          });
          if (hasUnionstore) {
            that.loadUnionStoreMenu();
          }

          that.SelfExtracEntry();

        } else {
          wx.showModal({
            title: '错误',
            content: '加载失败...',
            showCancel: false
          })
        }
      },
      fail: function(res) {
        wx.hideLoading()


      },
      complete: function(res) {


      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    wx.showLoading({
      title: '加载中...',
      mask: true
    });
  },

  /**
   * 绑定跳转订单列表
   */
  bindOrderTap: function(event) {
    var status = event.currentTarget.dataset.status;
    //添加缓存，用缓存来接收参数
    wx.setStorageSync("OrderStatusUserInfo", status)
    wx.switchTab({
      url: '/pages/myorders/myorders?status=' + status,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    common.pageInit(function() {
      var user = wx.getStorageSync('UserInfo');
      if (user == "" || user == undefined || user == null) {
        wx.redirectTo({
          url: '/pages/login/login',
        })
        return false;
      }
    
      that.bindData(that);

    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    //上拉  
    console.log("上拉")
    var that = this;
    that.bindData(that);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 加载盟店菜单开始
   */
  loadUnionStoreMenu: function() {
    var that = this;
    wx.request({
      url: appData.apiurl + "mobile/api/Unionstore/GetShop",
      method: "GET",
      data: {
        UserId: wx.getStorageSync('UserInfo').UserID,
        CustId: appData.custID
      },
      header: {
        "content-type": "application/json"
      },
      success: function(res) {
        if (res.data.code == 1200 && res.data.content != null) {
          var ls = [{
            ID: 0,
            MenuUrl: "/pages/MyShop/MyShop",
            MenuName: "我的店铺",
            Img: "/image/unionshop.png"
          }];
          that.setData({
            Menus: that.data.Menus.concat(ls),
            UnionShopMode: false
          });
        } else {
          that.GetSetting();
        }
      },
      fail: function(res) {


      },
      complete: function(res) {


      }
    })

  },
  GetSetting: function() {
    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/Unionstore/GetSetting",
      method: "GET",
      data: {
        CustId: appData.custID
      },
      header: {
        "content-type": "application/json"
      },
      success: function(res) {
        if (res.data.code == 1200 && res.data.content != null) {
          var ls = [{
            ID: 0,
            MenuUrl: "/pages/zshenqing/zshenqing",
            MenuName: "开通店铺",
            Img: "/image/unionshop.png"
          }];
          that.setData({
            Menus: that.data.Menus.concat(ls)
          });
        }

      },
      fail: function(res) {


      },
      complete: function(res) {


      }
    })
  }
  /**
   * 加载盟店菜单结束
   */

  ,
  //订单核销入口
  SelfExtracEntry: function () {
    var that = this;
    wx.request({
      url: appData.apiurl + "/mobile/api/SelfExtrac/IsSelfExtracOperater",
      method: "GET",
      data: {
        CustId: appData.custID, userID: wx.getStorageSync('UserInfo').UserID
        
      },
      header: {
        "content-type": "application/json"
      },
      success: function (res) {
        if (res.data.code == 1) {
          var ls = [{
            ID: 0,
            MenuUrl: "/pages/OrderPickedUp/OrderPickedUp",
            MenuName: "订单核销",
            Img: "/image/map.png"
          }];
          that.setData({
            Menus: that.data.Menus.concat(ls)
          });
        }

      },
      fail: function (res) {


      },
      complete: function (res) {


      }
    })
  }
})